package com.qingsong.mapper;

import com.github.pagehelper.PageInfo;
import com.qingsong.dto.PageDTO;
import com.qingsong.entity.Permission;
import com.qingsong.entity.Role;
import com.qingsong.entity.RolePermission;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface PermissionMapper {
    @Insert("INSERT INTO ums_permission (`title`,`url`,`icon`,`pid`,`info`) " +
            "VALUES" +
            " (#{title},#{url},#{icon},#{pid},#{info})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(Permission permission);

    @Select("select us.id, us.title, us.url, us.icon, us.pid, us.info from ums_permission us "+
            "where us.id = #{param1} and us.deleted = 0")
    Permission selectById(Long id);

    @Update("<script>" +
            "update ums_permission " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if>" +
            "<if test='url != null'> url = #{url}, </if>" +
            "<if test='icon != null'> icon = #{icon}, </if>" +
            "<if test='pid != null'> pid = #{pid}, </if>" +
            "<if test='info != null'> info = #{info}, </if>" +
            "<if test='deleted != null'> deleted = #{deleted}, </if>" +
            "<if test='created != null'> created = #{created}, </if>" +
            "<if test='updated != null'> updated = #{updated}, </if>" +
            "</set> " +
            "where id = #{id}" +
            "</script>")
    int updateById(Permission permission);

    @Update("<script>" +
            "update ums_permission set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Update("update ums_permission set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Results(id = "resultMap", value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "pid", property = "pid"),
            @Result(column = "pid", property = "parentPermission",
                    one = @One(select = "com.qingsong.mapper.PermissionMapper.selectById")),
    })
    @Select("select us.id, us.title, us.url, us.icon, us.pid, us.info, us.deleted, us.created, us.updated " +
            "from ums_permission us "+
            "where us.deleted = 0")
    List<Permission> list();

    @Select("select us.id, us.title, us.url, us.icon, us.pid,us.info " +
            "from ums_permission us "+
            "where us.deleted = 0")
    PageInfo<Permission> page(PageDTO pageDTO);

    @Select("select us.id, us.title, us.url, us.icon, us.pid, us.info " +
            "from ums_permission us " +
            "where us.title = #{title} ")
    Permission selectByAllTitle(Permission permission);

    @Delete("delete from ums_permission where id = #{param1}")
    int deleteById(Long id);

    @Select("select us.id, us.title, us.url, us.icon, us.pid,  us.info, " +
            "us.deleted, us.created, us.updated from ums_permission us "+
            "where us.deleted = 0 and us.title like concat('%', #{param1}, '%')")
    List<Permission> listLikeTitle(String title);

    @Select("select us.id, us.title, us.url, us.icon, us.pid, us.info, us.deleted, us.created, us.updated " +
            "from ums_permission us " +
            "where us.id in( " +
            "select urp.fk_permission_id from ums_role_permission urp " +
            "where fk_role_id in ( " +
            "select ue.fk_role_id from ums_emp_role ue where ue.fk_emp_id = #{param1}))")
    List<Permission> selectByEmpId(Long empId);

    @Select("select up.id, up.title, up.url, up.icon, up.pid, up.info, " +
            "up.deleted, up.created, up.updated from ums_role_permission urp " +
            "join ums_role ur on urp.fk_role_id = ur.id " +
            "join ums_permission up on urp.fk_permission_id = up.id " +
            "where urp.fk_role_id = #{param1} " +
            "and up.deleted = 0 and ur.deleted = 0 and urp.deleted = 0")
    List<Permission> selectByRoleId(Long roleId);

    @Update("update ums_role_permission set deleted = 1, updated = now() where fk_role_id = #{param1}")
    int loginDeleteByRoleId(Long roleId);
    @Insert("INSERT INTO ums_role_permission (`fk_role_id`,`fk_permission_id`,`deleted`,`created`,`updated`) " +
            "VALUES (#{fkRoleId},#{fkPermissionId},#{deleted},#{created},#{updated})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insertRolePermission(RolePermission rolePermission);

    @Select("select up.id, up.title, up.url, up.icon, up.pid, up.info, " +
            "up.deleted, up.created, up.updated from ums_permission up " +
            "where up.deleted = 0 and up.pid = 0")
    List<Permission> listParents();

    @Update("update ums_role_permission set deleted = 1, updated = now() where fk_permission_id = #{fkPermissionId}")
    void existsPermissionId(Long fkPermissionId);

    @Update("<script>" +
            "update ums_role_permission set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='us.fk_permission_id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    void existsPermissionIds(List<Long> fkPermissionIds);
}
